package com.nci.cloud.unit.management.FilePivotServerByBytes;

import io.netty.buffer.ByteBuf;
import io.netty.channel.*;

import io.netty.util.ReferenceCountUtil;

import java.io.File;
import java.io.RandomAccessFile;

public class FileServerHandler implements ChannelInboundHandler {

    int i = 0;
    RandomAccessFile rFile;

    @Override
    public void channelRegistered(ChannelHandlerContext channelHandlerContext) throws Exception {
        System.out.println("Channel Registered");
    }

    @Override
    public void channelUnregistered(ChannelHandlerContext channelHandlerContext) throws Exception {
        System.out.println("Channel Unregistered");
    }

    @Override
    public void channelActive(ChannelHandlerContext channelHandlerContext) throws Exception {
        System.out.println("Channel Active");
        File destFile = new File("d://a.exe");
        if(!destFile.exists()){
            destFile.createNewFile();
        }
        rFile = new RandomAccessFile("d://a.exe","rw");

       // System.out.println("Channel Active "+ channelHandlerContext.name());
       // RandomAccessFile localFile  = new RandomAccessFile("D:\\ideaIU-2017.3.3.exe","r");
       // ChunkedFile cf = new ChunkedFile(localFile,0,localFile.length(),5000);
       // channelHandlerContext.write(cf);
     /*   ChannelFuture sendFileFuture;
        sendFileFuture = */

      /*  sendFileFuture.addListener(new ChannelProgressiveFutureListener() {
            @Override
            public void operationProgressed(ChannelProgressiveFuture future, long progress, long total) {
                if (total < 0) { // total unknown
                    System.err.println(future.channel() + " Transfer progress: " + progress);
                } else {
                    System.err.println(future.channel() + " Transfer progress: " + progress + " / " + total);
                }
            }
            @Override
            public void operationComplete(ChannelProgressiveFuture future) {
                System.err.println(future.channel() + " Transfer complete.");
            }
        });*/

      //  channelHandlerContext.flush();
      //  System.out.println("send the file ");

    }/// channelActive

    @Override
    public void channelInactive(ChannelHandlerContext channelHandlerContext) throws Exception {
        System.out.println("Channel Inactive");
        rFile.close();
    }

    @Override
    public void channelRead(ChannelHandlerContext channelHandlerContext, Object o) throws Exception {
        System.out.println("File Server Read!");
        ByteBuf buf= (ByteBuf) o;
        byte[] content = new byte[buf.readableBytes()];
        i += buf.readableBytes();
        System.out.println("Channel Read "+i);
        buf.readBytes(content);
        rFile.write(content);
        // release the bytebuf
        ReferenceCountUtil.release(buf);

    }

    @Override
    public void channelReadComplete(ChannelHandlerContext channelHandlerContext) throws Exception {
    //   System.out.println("Channel Read Complete");
    }

    @Override
    public void userEventTriggered(ChannelHandlerContext channelHandlerContext, Object o) throws Exception {

    }

    @Override
    public void channelWritabilityChanged(ChannelHandlerContext channelHandlerContext) throws Exception {

    }

    @Override
    public void handlerAdded(ChannelHandlerContext channelHandlerContext) throws Exception {

    }

    @Override
    public void handlerRemoved(ChannelHandlerContext channelHandlerContext) throws Exception {

    }

    @Override
    public void exceptionCaught(ChannelHandlerContext channelHandlerContext, Throwable throwable) throws Exception {

    }


}
